<?php
/**
 * 教室信息类
 */
namespace app\admin\controller;

use think\App;
use think\facade\View;
use app\admin\model\Contentmodel;
use app\admin\model\Tagsmodel;
use app\admin\model\Usermodel;
use app\admin\model\Classroom;
use app\admin\model\Classifymodel;
use app\admin\validate\Content as validate;

class Content extends Common
{
    protected $id;
    protected $type;
    protected $cfmodel;
    protected $tagsmodel;
    protected $croommodel;
    protected $usermodel;

    public function __construct(App $app)
    {
        $this->validate = validate::class;
        $this->localModel = new Contentmodel();
        parent::__construct($app);
        $this->cfmodel = new Classifymodel();
        $this->tagsmodel = new Tagsmodel();
        $this->croommodel = new Classroom();
        $this->usermodel = new Usermodel();
        $this->id = $this->request->param('id', 0);
    }

    public function index()
    {
        $where = $whereExp = $showDataArr = [];

        $dataArr = $this->request->request('search/a', []);

        if($dataArr['title'])           $where[] = ['c.title', 'like', "%".$dataArr['title']."%"];
        if($dataArr['status'])          $where[] = ['c.status', '=', $dataArr['status']];
        if($dataArr['cid'])             $where[] = ['c.cid', '=', $dataArr['cid']];
        if($dataArr['examine_status'])  $where[] = ['examine_status', '=', $dataArr['examine_status']];
        if($dataArr['lid'])             $where[] = ['c.lid', '=', $dataArr['lid']];
        if($dataArr['happen_time'])     $where[] = ['c.happen_time', '=', $dataArr['happen_time']];
        if($dataArr['tags']){
            foreach($dataArr['tags'] as $tag){
                $whereExp[] = 'FIND_IN_SET('.$tag.', tags)';
            }
        }

        switch($this->request->controller()){
            case 'Article':    $type = 1;break;
            case 'Video':      $type = 2;break;
            case 'Courseware': $type = 5;break;
            case 'Course':     $type = 6;break;
            case 'Strcontent': $type = 7;break;
        }
        //$type = $this->request->controller() == 'Article' ? 1 : ($this->request->controller() == 'Video' ? 2 : 3);

        $tagsWhere = [['status', '=', 1]];

        $cfWhere = $this->request->controller() == 'Strcontent' ? [['status', '=', 1], ['level', '=', 2], ['fid', '=', 22]] : ('Courseware' ? [['status', '=', 1], ['level', '=', 2], ['fid', '=', 32]] : [['status', '=', 1], ['level', '=', 1], ['id', '<>', 18], ['id', '<>', 22]]);
        if(session('adminUserInfo.admintype') == 3){
            if(session('adminUserInfo.rid') == 4 || session('adminUserInfo.rid') == 3 || session('role_examine_num.admintype')) {
                //教室端教室管理员、审核可以看到本教室全部内容和公共内容
                $where[] = [['c.crid', 'in', session('adminUserInfo.cid').', 0']];
            }else{
                //教室端内容管理员、教师、其他角色只能看属于自己的内容
                $where[] = [['c.uid', 'in', session('adminUserInfo.uid')]];
            }
            $cfWhere[] = ['cid', 'in', session('adminUserInfo.cid').', 0'];
        }else{
            if(session('adminUserInfo.rid') == 7){
                //管理端代理只能看到公共个名下教室全部
                $showData['croomData'] = $this->croommodel->findAllInfo('id, name', [['agent_uid', '=', session('adminUserInfo.uid')]]);
                if($showData['croomData']){
                    foreach($showData['croomData'] as $crVal) $crids[] = $crVal['id'];
                    $where[] = [['c.crid', 'in', implode(',', $crids).', 0']];
                    $cfWhere[] = ['cid', 'in', implode(',', $crids).', 0'];
                }else{
                    $where[] = [['c.crid', 'in', '0']];       //没有教室信息则查看公共信息
                    $cfWhere[] = ['cid', 'in', '0'];
                }
            }else{
                //管理端其他角色用户可以看全部
                $showData['croomData'] = $this->croommodel->findAllInfo('id, name', [['status', '=', 1]]);
            }
        }

        //获取用户信息
//        $userWhere = [['status', '=', 1]];
//        if(session('adminUserInfo.cid')){
//            $userWhere[] = ['cid', '=', session('adminUserInfo.cid')];
//        }
//        $userData = $this->usermodel->findAllInfo('id, username', $userWhere);

        $showData['cfData'] = $this->cfmodel->findAllInfo('id, name', $cfWhere);
        $showData['tagsData'] = $this->tagsmodel->findAllInfo('id, title', $tagsWhere);
        if($dataArr['lid'])  $showData['scfData'] = $this->cfmodel->findAllInfo('id, name', [['status', '=', 1], ['level', '=', 2], ['fid', '=', $dataArr['cid']]]);
        $list = $this->localModel->findAll($type, $where, $this->request->param(), implode(' and  ', $whereExp));
        $showDataArr = ['data'=>$list['data'], 'pageData'=>$list['page'], 'showData'=>$showData];
        View::assign(array_merge($dataArr, $showDataArr));
    }

    public function add(){
        $tagsWhere = [['status', '=', 1]];
        $cfWhere = $this->request->controller() == 'Strcontent' ? [['status', '=', 1], ['level', '=', 2], ['fid', '=', 22]] : ('Courseware' ? [['status', '=', 1], ['level', '=', 2], ['fid', '=', 32]] : [['status', '=', 1], ['level', '=', 1], ['id', '<>', 18], ['id', '<>', 22]]);
        if(session('adminUserInfo.admintype') != 3){
            if(session('adminUserInfo.rid') == 7){
                //管理端代理只能看到公共个名下教室全部
                $showData['croomData'] = $this->croommodel->findAllInfo('id, name', [['agent_uid', '=', session('adminUserInfo.uid')]]);
                if($showData['croomData']){
                    foreach($showData['croomData'] as $crVal) $crids[] = $crVal['id'];
                    $where[] = [['c.crid', 'in', implode(',', $crids).', 0']];
                    $cfWhere[] = ['cid', 'in', implode(',', $crids).', 0'];
                }else{
                    $where[] = [['c.crid', 'in', '0']];       //没有教室信息则查看公共信息
                    $cfWhere[] = ['cid', 'in', '0'];
                }
            }else{
                //管理端其他角色用户可以看全部
                $showData['croomData'] = $this->croommodel->findAllInfo('id, name', [['status', '=', 1]]);
            }
        }else{
            $cfWhere[] = ['cid', 'in', session('adminUserInfo.cid').', 0'];
        }

        $showData['cfData'] = $this->cfmodel->findAllInfo('id, name', $cfWhere);
        $showData['tagsData'] = $this->tagsmodel->findAllInfo('id, title', $tagsWhere);

        return $showData;
    }

    public function save(){
        $dataArr = $this->request->request('info/a', []);
        $otherArr = $this->request->request('other/a', []);
        $courseArr = $this->request->request('course/a', []);
        $otherArr['content'] = $this->request->post('editorValue');
        if($dataArr['tags'])  $dataArr['tags'] = implode(',', $dataArr['tags']);

        if($dataArr['title'])  $info = $this->localModel->findDetaiInfoByOthers('id', ['title'=>$dataArr['title'], 'is_delete'=>1]);
        if(($this->id && $info && $this->id != $info[0]['id']) || (!$this->id && $info))  return json(['result'=>'fail', 'title'=>'标题已存在,请重新填写！']);
        if($msg = $this->checkData(array_merge($dataArr, $otherArr), $this->validateType))  return json(['result'=>'fail', 'title'=>$msg]);

        $dataArr['type'] = $this->type;
        $res = $this->localModel->saveInfo($dataArr, $otherArr, $courseArr, $this->id);

        if(!$res)  return json(['result'=>'fail', 'title'=>'操作失败！']);

        $this->logsData = ['cid'=>$this->id ? $this->id : $res, 'action'=>$this->id ? 'edit' : 'add', 'msg'=>$dataArr['title']];
        return json(['result'=>'success', 'title'=>'恭喜您操作成功！', 'url'=>(string)url($this->request->controller().'/index')]);
    }


}
